<template>
  <view class="shareMain">
    <!-- <view class="shareName">{{ name }}的推广码</view> -->
    <image :src="srcUrl"></image>
    <!-- <view class="shareBtnMain"><text @click="show = true">分享给您的客户</text></view> -->
    <!-- <view class="shareBtnMain"><text  @click="savePic">保存到手机</text></view> -->
    <!-- <u-popup v-model="show" mode="bottom">
			<view class="shareBtnBox">
				<u-icon class="close" name="close" color="#fb4535" size="32" @click="show = false"></u-icon>
				<view class="shareWayBox flex align-center justify-around">
					<view class="shareWayItem flex align-center" @click="shareFriend">
						<u-icon class="xIcon" name="weixin-fill" color="#04BE02" size="48"></u-icon>
						<text>分享到微信</text>
					</view>
					<view class="shareWayItem flex align-center" @click="savePic">
						<u-icon class="xIcon" name="download" color="#488ff0" size="48"></u-icon>
						<text>保存到手机</text>
					</view>
				</view>
			</view>
		</u-popup> -->
    <toast></toast>
  </view>
</template>

<script>
export default {
  data() {
    return {
      srcUrl: "",
      name: "",
      show: false,
    };
  },
  onLoad() {},
  onShow() {
    this.name = uni.getStorageSync("loginInfo").real_name;
    this.getShareCodeTap();
  },
  methods: {
    getShareCodeTap() {
      this.$u.api
        .spreadCode()
        .then((res) => {
          if (res.status == 200) {
            if (res.result) {
              let result = res.result.replace(/[\r\n]/g, "");
              this.srcUrl = result;
            } else {
              this.$refs.uToast.show({
                title: "推广码获取失败，请重试",
                type: "error",
                duration: 1500,
              });
              setTimeout(() => {
                this.$u.route({
                  type: "navigateBack",
                });
              }, 2000);
            }
          } else {
            this.$refs.uToast.show({
              title: res.msg,
              type: "primary",
            });
          }
        })
		.catch((res) => {
			if (res.errMsg == "request:fail timeout") {
				this.$u.toast("请求超时，请重试!");
			} else if (res.errMsg == "request:fail") {
				this.$u.toast("请求失败，请重试");
			} else {
				 this.$u.toast("请求失败，请重试");
			}
			this.$loading(false);
			setTimeout(() => {
				this.$u.route({
					type: "navigateBack",
					delta: 1,
				});
			}, 2000);
		});
    },
    //分享给微信朋友
    shareFriend() {
      uni.share({
        provider: "weixin",
        scene: "WXSceneSession",
        type: 2,
        imageUrl: this.srcUrl,
        success: function (res) {
          uni.showToast({
            title: "分享成功～",
          });
        },
        fail: function (err) {
          console.log("fail:" + JSON.stringify(err));
        },
      });
    },
    savePic() {
      uni.showLoading({
        title: "正在保存",
      });
      uni.saveImageToPhotosAlbum({
        filePath: this.srcUrl,
        success: function () {
          uni.showToast({
            title: "图片保存成功～",
          });
        },
        fail: function (e) {
          //TODO
        },
        complete: function () {
          uni.hideLoading();
        },
      });
    },
    onNavigationBarButtonTap(e) {
      const index = e.index;
      if (index == 0) {
        this.show = true;
      }
    },
  },
};
</script>

<style lang="scss">
page {
  background: #f52f2a url("../../static/images/shareBg.jpg") no-repeat center
    top;
  background-size: 100% auto;
  position: relative;
  height: 100%;
  .shareName {
    text-align: center;
    font-size: 32rpx;
    width: 100%;
    color: #f52f2a;
    position: absolute;
    left: 0;
    top: 528rpx;
    z-index: 9;
  }
  image {
    width: 280rpx;
    height: 280rpx;
    display: block;
    position: absolute;
    left: 50%;
    margin-left: -140rpx;
    top: 584rpx;
    z-index: 9999;
  }
}
// .shareMain {
// 	image {
// 		width:256rpx;
// 		height:256rpx;
// 		display: block;
// 		position:absolute;
// 		left:50%;
// 		margin-left:-128rpx;
// 		top:598rpx;
// 		z-index:9;
// 	}
// 	.shareBtnMain {
// 		padding: 20rpx;
// 		position: fixed;
// 		width: 100%;
// 		box-sizing: border-box;
// 		left: 0;
// 		bottom: 0;
// 		text {
// 			display: block;
// 			width: 100%;
// 			height: 80rpx;
// 			background: #fff;
// 			color: #fb4535;
// 			text-align: center;
// 			line-height: 80rpx;
// 			font-size: 32rpx;
// 			border-radius: 10rpx;
// 		}
// 	}
// }

.shareBtnBox {
  padding: 30rpx;
  position: relative;

  .close {
    position: absolute;
    right: 30rpx;
    top: 30rpx;
  }

  .shareWayBox {
    padding: 60rpx 0 50rpx;

    .shareWayItem {
      .xIcon {
        margin-right: 10rpx;
      }

      text {
        font-size: 28rpx;
      }
    }
  }
}
</style>
